package com.cos66.asynctask;

import java.io.InputStream;
import java.util.List;

import android.os.Handler;
import android.os.Message;

import com.cos66.intf.ErrorCallBack;
import com.cos66.intf.ListCallBack;
import com.cos66.store.HotActivityListDataFromXml;
import com.cos66.utils.Cos66Setting;
import com.cos66.utils.HttpUtils;

public class HotAcivityAsynTask {

	private static final int ERROR_MESSAGE = 0;
	private static final int SUCCEED_MESSAGE = 1;

	private String URL="";
	
	public HotAcivityAsynTask(final String URL){
		this.URL = URL;
	}
	
	public void LoadList(final ListCallBack listCallBack, final ErrorCallBack errorCallBack) {

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case ERROR_MESSAGE:
					errorCallBack.ErrorShow((String) msg.obj);
					break;
				case SUCCEED_MESSAGE:
					listCallBack.ListShow((List)msg.obj);
					break;
				}
			}

		};

		new Thread() {

			@Override
			public void run() {
				InputStream inputStream = HttpUtils.queryStringForGet(URL);
				if (inputStream == null) {
					Message message = handler.obtainMessage(ERROR_MESSAGE,
							Cos66Setting.SERVICE_ERROR);
					handler.sendMessage(message);
				} else {
					HotActivityListDataFromXml listDataFromXml = new HotActivityListDataFromXml(
							inputStream);
					Message message = handler.obtainMessage(SUCCEED_MESSAGE,
							listDataFromXml.getData());
					handler.sendMessage(message);
				}

			}

		}.start();

	}

}
